package org.tinygroup.trans.xstream;

import java.util.List;

import org.tinygroup.logger.LogLevel;
import org.tinygroup.logger.Logger;
import org.tinygroup.logger.LoggerFactory;
import org.tinygroup.vfs.FileObject;
import org.tinygroup.vfs.VFS;
import org.tinygroup.xstream.XStreamFactory;
import org.tinygroup.xstream.config.XStreamConfiguration;

import com.thoughtworks.xstream.XStream;

public class XStreamTransManager {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(XStreamTransManager.class);

	public static void initXStream(String filePath) {
		XStream stream = new XStream();
		stream.autodetectAnnotations(true);
		stream.alias("xstream-configuration", XStreamConfiguration.class);
		FileObject fileObject = VFS.resolveFile(filePath);
		XStreamConfiguration xStreamConfiguration = (XStreamConfiguration) stream
				.fromXML(fileObject.getInputStream());
		try {
			XStreamFactory.parse(xStreamConfiguration);
		} catch (Exception e) {
			LOGGER.logMessage(LogLevel.ERROR, "xstream配置文件初始化失败，文件路径：{0}",
					filePath);
			throw new RuntimeException("xstream配置文件:" + filePath + "初始化失败");
		} finally {
			VFS.closeInputStream(fileObject);
		}
	}

	public static void initXStream(List<String> filePaths) {
		for (String filePath : filePaths) {
			initXStream(filePath);
		}
	}

}
